package cn.sms.org.service.impl;

import cn.sms.basic.exception.BusinessException;
import cn.sms.basic.service.impl.BaseServiceImpl;
import cn.sms.org.domain.Employee;
import cn.sms.org.domain.Shop;
import cn.sms.org.mapper.EmployeeMapper;
import cn.sms.org.mapper.ShopMapper;
import cn.sms.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private EmployeeServiceImpl employeeServiceImpl;

    @Override
    public void settledIn(Shop shop) {
        Employee admin = shop.getAdmin();
        if(!admin.getPassword().equals(admin.getComfirmPassword())){
            throw new BusinessException("密码与确认密码不一致，请重新输入密码");
        }
        Employee employee = employeeMapper.byUsername(admin.getUsername());
        if(employee != null){
            throw new BusinessException("该用户已存在，不再需要注册");
        }
        //employeeMapper.save(admin);
        employeeServiceImpl.add(admin);
        shop.setAdmin_id(admin.getId());
        shopMapper.save(shop);
        shop.getId();
        admin.setShop_id(shop.getId());
        employeeMapper.update(admin);
    }
}
